<?php
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/7/15
 * Time: 上午10:17
 */

namespace Hilaoyu\Utils;


class UtilDateTime
{

    /**
     * 秒数转换成时分秒
     *
     * @param $seconds 秒数
     *
     * @return string
     */
    public static function secondsToHis($seconds, $htmlTag = '')
    {

        $htmlTag = trim($htmlTag);

        $htmlTagStart = $htmlTagEnd = '';
        if ($htmlTag) {
            $htmlTagStart = '<' . $htmlTag . ' class="_time_class_">';
            $htmlTagEnd = '</' . $htmlTag . '>';
        }

        $str = $seconds < 0 ? str_replace('_time_class_', 'time_tag_prefix', $htmlTagStart).'-'.$htmlTagEnd : '';

        $seconds = abs($seconds);

        $time_data = static::calculate($seconds);
        $hour = $time_data['hour']; // 小时
        $minute = $time_data['minute']; // 分钟
        $second = $time_data['second']; // 秒

        //小时
        if ($hour) {
            $str .= str_replace('_time_class_', 'time_num time_num_hour', $htmlTagStart) . $hour . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_hour', $htmlTagStart).'小时'.$htmlTagEnd;
        }

        //分钟
        $str .= str_replace('_time_class_', 'time_num time_num_minute', $htmlTagStart) . $minute . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_minute', $htmlTagStart).'分'.$htmlTagEnd;
        //second

        $str .= str_replace('_time_class_', 'time_num time_num_second', $htmlTagStart) . $second . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_second', $htmlTagStart).'秒'.$htmlTagEnd;


        return $str;
    }


    /**
     * 秒数转换成*天*小时*分钟*秒
     * @param int $seconds          秒数
     * @param string $htmlTag       标签名 eg：span
     * @param bool $placeholder     天数和小时是否占位 eg：true:0天0小时10分12秒; false:10分10秒
     * @param int $show_day_hour    超过多少小时显示天数  小于24小时无意义 eg：24(1天2小时10分30秒) | 72(26小时10分30秒)
     * @return string
     */
    public static function secondsToDHis(int $seconds, string $htmlTag = '', bool $placeholder=false, int $show_day_hour = -1)
    {

        $htmlTag = trim($htmlTag);

        $htmlTagStart = $htmlTagEnd = '';
        if ($htmlTag) {
            $htmlTagStart = '<' . $htmlTag . ' class="_time_class_">';
            $htmlTagEnd = '</' . $htmlTag . '>';
        }

        $str = $seconds < 0 ? str_replace('_time_class_', 'time_tag_prefix', $htmlTagStart).'-'.$htmlTagEnd : '';

        $seconds = abs($seconds);

        $time_data = static::calculate($seconds, $show_day_hour);
        $day = $time_data['day'];   // 天
        $hour = $time_data['hour']; // 小时
        $minute = $time_data['minute']; // 分钟
        $second = $time_data['second']; // 秒


        //天
        if ($placeholder || $day) {
            $str .= str_replace('_time_class_', 'time_num time_num_day', $htmlTagStart) . $day . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_day', $htmlTagStart).'天'.$htmlTagEnd;
        }

        //小时
        if ($placeholder || $hour || $day) {
            $str .= str_replace('_time_class_', 'time_num time_num_hour', $htmlTagStart) . $hour . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_hour', $htmlTagStart).'小时'.$htmlTagEnd;
        }

        //分钟
        $str .= str_replace('_time_class_', 'time_num time_num_minute', $htmlTagStart) . $minute . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_minute', $htmlTagStart).'分'.$htmlTagEnd;
        //second

        $str .= str_replace('_time_class_', 'time_num time_num_second', $htmlTagStart) . $second . $htmlTagEnd . str_replace('_time_class_', 'time_unit time_unit_second', $htmlTagStart).'秒'.$htmlTagEnd;

        return $str;
    }

    /**
     *  时间格式化
     * @param int $timeint
     * @return string
     */
    public static function getHMSBySec($timeint = 0): string
    {

        $H = floor($timeint / 3600);
        $M = floor(($timeint - $H * 3600) / 60);
        $S = $timeint % 60;

        $str = $H > 0 ? $H . '小时' : '';
        $str .= ($M > 0 || $H > 0) ? $M . '分' : '';
        $str .= $S . '秒';

        return $str;
    }


    private static function calculate($seconds, $show_day_hour=-1){

        $time_data = [
            'day' => 0,
            'hour' => 0,
            'minute' => 0,
            'second' => 0,
        ];

        // 小时
        $time_data['hour'] = floor($seconds / 3600);

        // 天数 超过指定小时数显示天数、并重新计算小时数
        if ($show_day_hour > 0 && $time_data['hour'] >= $show_day_hour) {
            $time_data['day'] = floor($seconds / 86400);

            $time_data['hour'] = floor($seconds % (86400) / 3600);
//            $time_data['hour'] = floor(($seconds - $time_data['day'] * 86400) / 3600);
        }

        // 分钟
        $time_data['minute'] = floor((($seconds % 86400) % 3600) / 60);

        // 秒
        $time_data['second'] = floor($seconds % 60);


        return $time_data;
    }


}
